package com.biaobiaoqi.scoreanalysis.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import com.biaobiaoqi.scoreanalysis.exception.DuplicateCourseException;
import com.biaobiaoqi.scoreanalysis.exception.NoSuchClazzTypeFoundException;
import com.biaobiaoqi.scoreanalysis.hibernate.bean.ClazzType;
import com.biaobiaoqi.scoreanalysis.hibernate.bean.Course;
import com.biaobiaoqi.scoreanalysis.service.IClazzTypeService;
import com.biaobiaoqi.scoreanalysis.service.ICourseService;

public class CourseServiceImpl extends DaoServiceImpl<Course> implements
		ICourseService {
	@Resource
	IClazzTypeService clazzTypeService;

	@Override
	public void addCourse(String name, int belongClazzType)
			throws DuplicateCourseException {
		if (findCourse(name) != null) {
			throw new DuplicateCourseException();
		}
		Course course = new Course();
		course.setName(name);
		course.setBelongClazzType(clazzTypeService
				.findClazzType(belongClazzType));
		getDao().create(course);
	}

	@Override
	public List<Course> listAllCourse(int start, int number) {
		return getDao().queryList("from Course c", start, number);
	}

	@Override
	public List<Course> listCourse(String clazzType)
			throws NoSuchClazzTypeFoundException {
		ClazzType type = clazzTypeService.findClazzType(clazzType);
		if (type == null) {
			throw new NoSuchClazzTypeFoundException();
		}
		return getDao().queryList("from Course c where c.clazzType=?",
				clazzType);
	}

	@Override
	public Course findCourse(String courseName) {
		return getDao().queryUnique("from Course c where c.name=?", courseName);
	}

	@Override
	public Course findCourse(int courseId) {
		return getDao().findById(Course.class, courseId);
	}

	@Override
	public void deleteCourse(int courseId) {
		getDao().delete(findCourse(courseId));
	}

}
